package com.education.api.controller.admin.system;

import com.education.business.service.system.ExamRoomStudentService;
import com.education.common.utils.Result;
import com.education.model.dto.ExamRoomStudentDto;
import com.education.model.dto.StudentInfoDto;
import com.education.model.entity.ExamRoomStudent;
import com.education.model.request.ExamRoomStudentRequest;
import com.education.model.request.PageParam;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/examroomstudent")
public class ExamRoomStudentController {
    @Autowired
    private ExamRoomStudentService examRoomStudentService;


    //考生考场
    @GetMapping("/getExamRoomStudentCount")
    public Result getExamRoomStudentCount(@RequestParam Integer examRoomId){

        return Result.success(examRoomStudentService.getExamRoomStudentCount(examRoomId));
    }
    /**
     * 考场考生列表
     * @param pageParam
     * @param
     * @return
     */

//    @RequiresPermissions("system:grade:list")
     @RequestMapping(value = "/getExamRoomStudentList",method = RequestMethod.GET)
    public Result list(PageParam pageParam, ExamRoomStudentDto examRoomStudent) {
        return Result.success(examRoomStudentService.selectPageList(pageParam, examRoomStudent));
    }
    /**
     *
     * @param  @ApiOperation("根据id批量删除用户")
     * @param
     * @return
     */
    //@ResponseBody
    @GetMapping(value = "/delectStudentAllById")
    public int deleteStudentAllById(@ApiParam(name = "id",value = "考生id",required = true) @RequestParam(required = true) String id){
        System.out.println(id);
        List idList = new ArrayList();
        String[] strs = id.split(",");
        for (String str:strs){
            idList.add(str);
        }
        return examRoomStudentService.deleteStudentAllById(idList);
    }

    /**
     * 批量增加
     * @param
     * @return
     */

    @PostMapping(value = "/addSelectStudent")
    public Result addSelectStudent(@RequestBody ExamRoomStudentRequest roomStudent) {
        System.out.println("roomStudent.getRoomId()"+roomStudent.getRoomId());
        System.out.println(roomStudent.getExamTaskId());
        List<ExamRoomStudent> studentList = new ArrayList<>();

        for (ExamRoomStudentDto s: roomStudent.getStudents()) {
            ExamRoomStudent examRoomStudent = new ExamRoomStudent();
            examRoomStudent.setExamRoomId(roomStudent.getRoomId());
            examRoomStudent.setStudentInfoId(s.getId());
            examRoomStudent.setExamTaskId(roomStudent.getExamTaskId());
            studentList.add(examRoomStudent);
        }
        return Result.success( 1,"添加成功",examRoomStudentService.saveOrUpdateBatch(studentList));
    }
    /**
     * 单个删除
     */
    @GetMapping(value = "/deleteExamRoomStudentById")
    public Result deleteExamRoomStudentById(@RequestParam("id") Integer id){
        return Result.success(examRoomStudentService.deleteById(id));
    }


}
